package com.zf.shuashua.common;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;


public class PhoneCallService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        PhoneStateListener psl = new PhoneStateListener() {
            //当检测到电话状态发生改变的时候，会自动调用执行方法
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);
                switch (state) {
                    case TelephonyManager.CALL_STATE_IDLE:// 电话挂断
                        PhoneCallManager.getInstance().onCallIdle();
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK: //电话通话的状态
                        PhoneCallManager.getInstance().onCallOffHook();
                        break;
                    case TelephonyManager.CALL_STATE_RINGING: //电话响铃的状态
                        PhoneCallManager.getInstance().onCallRinging();
                        break;
                }
            }
        };
        TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        tm.listen(psl, PhoneStateListener.LISTEN_CALL_STATE);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}